package com.yeyks.controller.admin;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.dto.order.AppOrderInfoDTO;
import com.yeyks.common.appcommon.dto.order.AppOrderStatusDTO;
import com.yeyks.common.appcommon.param.order.GetOrderDetailParam;
import com.yeyks.common.appcommon.param.order.QueryOrderParam;
import com.yeyks.common.appcommon.param.order.UpdateCheckInParam;
import com.yeyks.common.appcommon.result.CommonAuthHeader;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.em.order.OrderSourceTypeEnum;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.List;

import static com.yeyks.common.constants.SysLogMsg.*;
import static com.yeyks.common.utils.apputil.enums.RefundTypeEnum.USER;

/**
 * @author Administrator
 */
@Api(tags = "AppOrderController_订单接口", description = "订单的相关接口")
@RestController
@RequestMapping("/admin/order")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppOrderController extends BaseController {


    private final OrderHeadService orderHeadService;

    /**
     * 待处理, 全部,待处理, 其他条件等..分页查询订单
     * 根据酒店查询
     */

    @ApiOperation("分页查询订单,根据 待处理, 全部, 其他条件等..<C2B订单>")
    @PostMapping("/list")
    public CommonResult<CommonPage<AppOrderInfoDTO>> list(QueryOrderParam queryOrderParam) {

        AppAccountDTO appAccountDTO = this.validToken(queryOrderParam.getToken(),
                queryOrderParam.getVersion(), queryOrderParam.getPlatform());
        //设置为C2b订单
        queryOrderParam.setVipFlag(0);
        queryOrderParam.setSourceType(OrderSourceTypeEnum.YYKS.getValue());

        return orderHeadService.myAdminAppPageList(queryOrderParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("分页查询订单,根据 待处理, 全部, 其他条件等..<臻选VIP订单>")
    @PostMapping("/list/vip")
    public CommonResult<CommonPage<AppOrderInfoDTO>> listVip(QueryOrderParam queryOrderParam) {

        AppAccountDTO appAccountDTO = this.validToken(queryOrderParam.getToken(),
                queryOrderParam.getVersion(), queryOrderParam.getPlatform());
        //设置为VIP订单
        queryOrderParam.setVipFlag(1);
        queryOrderParam.setSourceType(OrderSourceTypeEnum.YYKS.getValue());

        return orderHeadService.myAdminAppPageList(queryOrderParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("分页查询订单,根据 待处理, 全部, 其他条件等..<优享VIP订单> (1.7.0)")
    @PostMapping("/list/min/vip")
    public CommonResult<CommonPage<AppOrderInfoDTO>> listMinVip(QueryOrderParam queryOrderParam) {

        AppAccountDTO appAccountDTO = this.validToken(queryOrderParam.getToken(),
                queryOrderParam.getVersion(), queryOrderParam.getPlatform());
        //设置为VIP订单
        queryOrderParam.setVipFlag(3);
        queryOrderParam.setSourceType(OrderSourceTypeEnum.YYKS.getValue());

        return orderHeadService.myAdminAppPageList(queryOrderParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("分页查询CnBooking订单,根据 待处理, 全部, 其他条件等..<CNBooking订单> (1.7.0)")
    @PostMapping("/list/cn")
    public CommonResult<CommonPage<AppOrderInfoDTO>> listCn(QueryOrderParam queryOrderParam) {

        AppAccountDTO appAccountDTO = this.validToken(queryOrderParam.getToken(),
                queryOrderParam.getVersion(), queryOrderParam.getPlatform());
        //设置为CN订单
        queryOrderParam.setSourceType(OrderSourceTypeEnum.CNBOOKING.getValue());

        return orderHeadService.myAdminAppPageList(queryOrderParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }


    @ApiOperation("根据订单号查询订单详情 (1.7.0兼容老版本)")
    @PostMapping(value = "/getdetail")
    public CommonResult<AppOrderInfoDTO> getDetail(@Valid GetOrderDetailParam getOrderDetailParam) {

        AppAccountDTO appAccountDTO = this.validToken(getOrderDetailParam.getToken(),
                getOrderDetailParam.getVersion(), getOrderDetailParam.getPlatform());

        return orderHeadService.getAdminAppOrderDetail(getOrderDetailParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appOrderInfoService.getDetail(getOrderDetailParam);
    }

    /**
     * 确认入住
     */
    @AdminAppLog(value = ORDER_UPDATE_CHECKIN)
    @ApiOperation("确认入住")
    @PostMapping(value = "/checkin")
    public CommonResult checkIn(@Valid UpdateCheckInParam updateCheckInParam) {

        AppAccountDTO appAccountDTO = this.validToken(updateCheckInParam.getToken(),
                updateCheckInParam.getVersion(), updateCheckInParam.getPlatform());

        return orderHeadService.checkIn(updateCheckInParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }


    /**
     * 无法接单(退款 + 更改状态)
     */
    @AdminAppLog(value = ORDER_UPDATE_REFUND)
    @ApiOperation("无法接单(退款 + 更改状态), 只有酒店和总公司才能有这个按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/refund")
    public CommonResult refund(@RequestParam(value = "token", required = true) String token,
                               @RequestParam(value = "version", required = false) String version,
                               @RequestParam(value = "platform", required = false) String platform,
                               @RequestParam(value = "orderNo", required = true) String orderNo) {
        AppAccountDTO appAccountDTO = this.validToken(token, version, platform);

        try {
            /*orderHeadService.refund(MyBeanUtils.convert(appAccountDTO,AccountInfo.class),
                    orderNo, true, HOTEL.getDesc());*/
            orderHeadService.refundWeb(MyBeanUtils.convert(appAccountDTO,AccountInfo.class), orderNo);
        }catch (ServiceException e){
            throw new OftenFiledException(e.getMessage());
        }
        return CommonResult.success(null,"无法接单操作成功");
        //return appOrderInfoService.refund(token, orderNo, type, version, platform);
    }

    /**
     * 确认退款
     */
    @AdminAppLog(value = ORDER_CONFIRM_REFUND)
    @ApiOperation("确认退款")
    @PostMapping(value = "/confirm/refund")
    public CommonResult confirmRefund(@RequestParam(value = "token", required = true) String token,
                                      @RequestParam(value = "version", required = false) String version,
                                      @RequestParam(value = "platform", required = false) String platform,
                                      @RequestParam(value = "orderNo", required = true) String orderNo) {

        AppAccountDTO appAccountDTO = this.validToken(token, version, platform);

        try {
            /*orderHeadService.refund(MyBeanUtils.convert(appAccountDTO,AccountInfo.class),
                    orderNo, true, USER.getDesc());*/
            orderHeadService.refundWeb(MyBeanUtils.convert(appAccountDTO,AccountInfo.class), orderNo);
        }catch (ServiceException e){
            throw new OftenFiledException(e.getMessage());
        }
        return CommonResult.success(null,"确认退款成功");
        //return appOrderInfoService.confirmRefund(token, orderNo, version, platform);
    }


    /**
     * 拒绝退款
     */
    @AdminAppLog(value = ORDER_REFUSE_REFUND)
    @ApiOperation("拒绝退款")
    @PostMapping(value = "/refuse/refund")
    public CommonResult refuseRefund(@RequestParam(value = "token", required = true) String token,
                                     @RequestParam(value = "version", required = false) String version,
                                     @RequestParam(value = "platform", required = false) String platform,
                                     @RequestParam(value = "orderNo", required = true) String orderNo) {

        AppAccountDTO appAccountDTO = this.validToken(token, version, platform);

        try {
            //拒绝退款 传入false
            orderHeadService.refund(MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                    orderNo, false, USER.getDesc());
        }catch (ServiceException e){
            throw new OftenFiledException(e.getMessage());
        }
        return CommonResult.success(null,"拒绝退款操作成功");
    }

    @ApiOperation("获取订单【状态】列表接口")
    @PostMapping(value = "/get/status/list")
    public CommonResult<List<AppOrderStatusDTO>> getOrderStatusList(@Valid CommonAuthHeader param){

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());

        return orderHeadService.getOrderStatusList(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

}
